clear all 
set more off


* This file deals with the decomposition in Table 5.
* decomposes B/W separately.


* load
use "DecompositionResults_EUN_BWUN.dta", clear
tempfile dataio1
save `dataio1'

* list of transitions
local xlist "BU BN WU WN UB NB UW NW BW WB WWnew_jj"

local xcount = 1
foreach x of local xlist {

	use `dataio1', clear

	if "`x'" == "BU" | "`x'" == "BN" | "`x'" == "WU" | "`x'" == "WN" | "`x'" == "BW" | "`x'" == "WB" | "`x'" == "WWnew_jj" {
		local avar "g_easo"
	}
	else {
		local avar "g_eas"
	}

	display in red "------------------ CODE `x'"
	reg aggtrans_`avar'_`x' lcgdp
	local slope = _b[lcgdp]

	reg country_`avar'_`x' lcgdp
	local ce = _b[lcgdp]

	reg comp_`avar'_`x' lcgdp
	local co = _b[lcgdp]


	clear
	set obs 3

	gen name = "slope" if _n == 1
	replace name = "composition" if _n == 2
	replace name = "country" if _n == 3

	gen count = _n

	gen `x' = .
	replace `x' = `slope' if name == "slope"
	replace `x' = `co'/(`co' + `ce' ) if name == "composition"
	replace `x' = `ce'/(`co' + `ce') if name == "country"

	capture rename WWnew_occ OOw
	capture rename WWnew_jj JJw

	if `xcount' == 1 {
		tempfile dhold1
		save `dhold1'
	}
	else {
		merge 1:1 name using `dhold1'
		drop _merge

		tempfile dhold1
		save `dhold1'
	}	


	local xcount = `xcount' + 1

}




order count name BU BN WU WN UB NB UW NW BW WB JJw
sort count
drop count


tostring(BU BN WU WN UB NB UW NW BW WB JJw), replace force format("%15.2fc")

replace name = "Composition" if name == "composition"
replace name = "Country" if name == "country"
drop if name == "slope"
set obs 3

replace name = "Total" if name == ""
foreach x of varlist * {
	replace `x' = "1.000" if `x' == ""
}

order name BU BN WU WN UB NB UW NW BW WB JJw


tempfile datacutme0
save `datacutme0'


forvalues ii = 1/3 {

	use `datacutme0', clear

	* exit
	if `ii' == 1 {
		keep name BU BN WU WN
		local n2 "PanelA"

		gen vb1=.
		order vb1, after(BN)
	}
	*jfr 
	if `ii' == 2 {
		keep name UB NB UW NW
		local n2 "PanelB"		

		gen vb1=.
		order vb1, after(NB)		
	}
	*jobjob
	if `ii' == 3 {
		keep name BW WB JJw
		local n2 "PanelC"

		gen vb1=.
		order vb1, after(BW)		

		replace BW = "\multicolumn{2}{c}{"+BW+"}"
	}	



	#delimit ;
	texsave using "Figures/Table5_`n2'.tex", 
	title(Aggregate Flow Decomposition) 
	varlabels
	align(l c c c)
	nofix
	footnote("\textit{Table notes:} Decomposition for aggregate flows, normalized by average across all country-years. Decomposition terms are given as percentages of that overall slope with log GDP per capita. Exit rates and Job-Job based on full set of interactions Age 
	\$ \times \$ Education \$ \times \$ Sex \$ \times \$ Occupation. Job-finding rates uses Age 
	\$ \times \$ Education \$ \times \$ Sex.") 
	replace;
	#delimit cr	




}


